﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WxOpenPlatLibCore
{
    sealed public class DeserializeJson : Dictionary<string, object>
    {
        private Dictionary<string, object> _all_keyVal = new Dictionary<string, object>();

        private void GetElement(string key, JToken json)
        {
            string _type = json.GetType().Name;
            if (_type == "JProperty")
            {
                var _item = json as JProperty;
                GetElement((key + "." + _item.Name).Trim('.'), _item.Value);
            }
            else if (_type == "JObject")
            {
                foreach (var property in (json as JObject).Properties())
                {
                    GetElement((key + "." + property.Name).Trim('.'), property.Value);
                }
            }
            else if (_type == "JArray")
            {
                var _item = json as JArray;
                foreach (JObject child in (json as JToken).Children())
                {
                    foreach (var property in (child as JObject).Properties())
                    {
                        GetElement((key + "." + property.Name).Trim('.'), property.Value);
                    }
                }
            }
            else
            {
                if (!_all_keyVal.ContainsKey(key))
                {
                    _all_keyVal.Add(key, (json as JValue).Value);
                }
                else
                {
                    _all_keyVal[key] = _all_keyVal[key] + "," + (json as JValue).Value;
                }
            }

            //Dictionary<string, object> data = _DicData as Dictionary<string, object>;
            //if (null != data)
            //{
            //    foreach (var item in data)
            //    {
            //        string _type = item.Value.GetType().Name;
            //        string _newkey = key + "." + item.Key;
            //        if (_type == ("JObject"))
            //        {

            //            GetElement(_newkey.Trim('.'), item.Value);
            //        }
            //        else if (_type == ("Array"))
            //        {
            //            System.Collections.ArrayList _array = item.Value as System.Collections.ArrayList;
            //            if (_array != null)
            //            {
            //                _all_keyVal.Add(_newkey.Trim('.') + ".length", _array.Count);
            //                int ind = 0;
            //                foreach (var itm in _array)
            //                {
            //                    string __type = itm.GetType().Name;
            //                    if (__type == ("JObject"))
            //                    {
            //                        GetElement((_newkey + "." + ind++).Trim('.'), itm);
            //                    }
            //                    else
            //                    {
            //                        _all_keyVal.Add((_newkey + "." + ind++).Trim('.'), itm);
            //                    }
            //                    //Response.Write(itm+"<br>");
            //                }
            //            }
            //        }
            //        //else if (_type == "String" || _type == "Int32")
            //        //{
            //        //    _all_keyVal.Add(_newkey.Trim('.'), item.Value);
            //        //    //Response.Write(item.Key + "-->" + item.Value + "<br>");
            //        //}
            //        else
            //        {
            //            _all_keyVal.Add(_newkey.Trim('.'), item.Value);
            //            //Response.Write(item.Key + "-->" + item.Value + "<br>");
            //        }
            //    }
            //}
        }

        public DeserializeJson GetResult(string _jsonstring)
        {
            _all_keyVal = new Dictionary<string, object>();
            _all_keyVal.Clear();

            JToken result = JObject.Parse(_jsonstring);

            GetElement(string.Empty, result);
            this.Clear();
            foreach (var item in _all_keyVal)
            {
                this.Add(item.Key, item.Value);
            }
            return this;
            //return _all_keyVal;
        }

        public object GetObject(string _key)
        {
            if (this.Keys.Contains(_key))
            {
                return this[_key];
            }
            else
            {
                return null;
            }
        }

        public string GetString(string _key)
        {
            if (this.Keys.Contains(_key))
            {
                return this[_key].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public Int32 GetInt32(string _key)
        {
            if (this.Keys.Contains(_key))
            {
                return Int32.Parse(this[_key].ToString());
            }
            else
            {
                return 0;
            }
        }

        public Int32 GetInt32(string _key, Int32 _default)
        {
            if (this.Keys.Contains(_key))
            {
                return Int32.Parse(this[_key].ToString());
            }
            else
            {
                return _default;
            }
        }

        public string Output()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("DeserializeJson result = new DeserializeJson().GetResult(str);");
            sb.Append("foreach (var item in result)");
            sb.Append("{");
            sb.Append("    Response.Write(item.Key + \"-->\" + result.GetObject(item.Key) + \"<br>\");");
            sb.Append("}");
            sb.Append("Response.Write(\"aaa-->\" + result.GetInt32(\"aaa\",1000) + \"<br>\")");
            return sb.ToString();
        }

    }
}
